"""
@Author : 合肥一元数智教育科技有限公司
@Date :  2025/5/16 8:33
@Description : 
 推导式的用法    引入推导式可以在某些场景下简化代码的写法 让代码更加简洁

 1. 列表推导式
 2. 字典推导式
 3.元组推导式     generator  ->  tuple(generator)   -> tuple
 4.集合推导式
"""
lst = [1, 2, 3, 4]
# 重复
lst_new = lst * 2
print(lst_new)

# lst 列表中的元素*2  存入新列表
"""
new_lst = []
for i in lst:
    new_lst.append(i * 2)
print(new_lst)
"""

# 引入列表推导式 简化以上操作  for i in 可遍历的元素
new_lst = [i * 2 for i in lst]
print(new_lst)

str_info = 'hello'
str_lst = [i for i in str_info]
print(str_lst)

#  列表中的元素是 1-100之间的数字
num_lst = [i for i in range(1, 101)]
print(num_lst)

# 列表中的元素要是偶数    if i % 2 == 0   i 偶数   true
num_lst = [i for i in range(1, 101) if i % 2 == 0]
print(num_lst)

# 字符串转大写
str_lst_new = ['hello', 'world', 'python', 'html', 'css']
str_upper = [str.upper() for str in str_lst_new]
print(str_upper)

# 字典推导式  {'hello':5,'world':5}
str_lst_new = ['hello', 'world', 'python', 'html', 'css']
str_dict = {str: len(str) for str in str_lst_new}
print(str_dict)

# 统计字符个数大于3的字符串
str_dict = {str: len(str) for str in str_lst_new if len(str) > 3}
print(str_dict)

# 元组推导式
tup_num = (1, 2, 3, 4)

# tup_num 中的元素 +2  放在新元组中
new_tup = (num + 2 for num in tup_num)  # <generator object <genexpr> at 0x000002DC2F44A570>
print(new_tup, type(new_tup))
new_tup = tuple(new_tup)  # generator   -> tuple
print(new_tup, type(new_tup))

# 集合推导式
num_set = {1, 2, 3, 4}
new_num_set = {num + 3 for num in num_set}
print(new_num_set, type(new_num_set))
